<template>
  <a-layout class="layout">
    <!-- 侧边栏 -->
    <a-layout-sider
      v-model:collapsed="collapsed"
      :trigger="null"
      collapsible
      class="layout-sider"
      width="240"
    >
      <!-- Logo区域 -->
      <div class="logo">
        <img src="/favicon.ico" alt="Logo" class="logo-img" />
        <span v-if="!collapsed" class="logo-text">学生管理系统</span>
      </div>
      
      <!-- 导航菜单 -->
      <a-menu
        v-model:selectedKeys="selectedKeys"
        v-model:openKeys="openKeys"
        mode="inline"
        theme="light"
        :inline-collapsed="collapsed"
        @click="handleMenuClick"
      >
        <a-menu-item key="dashboard">
          <template #icon>
            <DashboardOutlined />
          </template>
          <span>仪表盘</span>
        </a-menu-item>
        
        <a-menu-item key="students">
          <template #icon>
            <UserOutlined />
          </template>
          <span>学生管理</span>
        </a-menu-item>
        
        <a-menu-item key="teachers">
          <template #icon>
            <TeamOutlined />
          </template>
          <span>教师管理</span>
        </a-menu-item>
        
        <a-menu-item key="classes">
          <template #icon>
            <ApartmentOutlined />
          </template>
          <span>班级管理</span>
        </a-menu-item>
        
        <a-menu-item key="courses">
          <template #icon>
            <BookOutlined />
          </template>
          <span>课程管理</span>
        </a-menu-item>
        
        <a-menu-item key="grades">
          <template #icon>
            <BarChartOutlined />
          </template>
          <span>成绩管理</span>
        </a-menu-item>
      </a-menu>
    </a-layout-sider>
    
    <a-layout>
      <!-- 头部 -->
      <a-layout-header class="layout-header">
        <div class="header-left">
          <a-button
            type="text"
            @click="toggleCollapsed"
            class="trigger"
          >
            <MenuUnfoldOutlined v-if="collapsed" />
            <MenuFoldOutlined v-else />
          </a-button>
          
          <!-- 面包屑导航 -->
          <a-breadcrumb class="breadcrumb">
            <a-breadcrumb-item>首页</a-breadcrumb-item>
            <a-breadcrumb-item v-if="currentPageTitle">{{ currentPageTitle }}</a-breadcrumb-item>
          </a-breadcrumb>
        </div>
        
        <div class="header-right">
          <!-- 用户信息 -->
          <a-dropdown>
            <a-space class="user-info">
              <a-avatar :size="32">
                <template #icon>
                  <UserOutlined />
                </template>
              </a-avatar>
              <span class="username">{{ userStore.userName || '管理员' }}</span>
              <DownOutlined />
            </a-space>
            
            <template #overlay>
              <a-menu @click="handleUserMenuClick">
                <a-menu-item key="profile">
                  <UserOutlined />
                  个人信息
                </a-menu-item>
                <a-menu-item key="changePassword">
                  <LockOutlined />
                  修改密码
                </a-menu-item>
                <a-menu-divider />
                <a-menu-item key="logout">
                  <LogoutOutlined />
                  退出登录
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
      </a-layout-header>
      
      <!-- 内容区域 -->
      <a-layout-content class="layout-content">
        <router-view />
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { Modal } from 'ant-design-vue'
import {
  DashboardOutlined,
  UserOutlined,
  TeamOutlined,
  ApartmentOutlined,
  BookOutlined,
  BarChartOutlined,
  MenuUnfoldOutlined,
  MenuFoldOutlined,
  DownOutlined,
  LockOutlined,
  LogoutOutlined
} from '@ant-design/icons-vue'
import { useUserStore, useAppStore } from '@/store'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const appStore = useAppStore()

// 侧边栏折叠状态
const collapsed = computed({
  get: () => appStore.collapsed,
  set: (value) => appStore.toggleSidebar()
})

// 菜单选中状态
const selectedKeys = ref(['dashboard'])
const openKeys = ref([])

// 当前页面标题
const currentPageTitle = computed(() => {
  const meta = route.meta
  return meta?.title || ''
})

// 监听路由变化，更新菜单选中状态
watch(
  () => route.name,
  (newName) => {
    if (newName) {
      selectedKeys.value = [newName.toLowerCase()]
    }
  },
  { immediate: true }
)

// 切换侧边栏
const toggleCollapsed = () => {
  appStore.toggleSidebar()
}

// 处理菜单点击
const handleMenuClick = ({ key }) => {
  router.push(`/${key}`)
}

// 处理用户菜单点击
const handleUserMenuClick = ({ key }) => {
  switch (key) {
    case 'profile':
      // 跳转到个人信息页面
      break
    case 'changePassword':
      // 打开修改密码弹窗
      break
    case 'logout':
      handleLogout()
      break
  }
}

// 退出登录
const handleLogout = () => {
  Modal.confirm({
    title: '确认退出',
    content: '确定要退出登录吗？',
    onOk() {
      userStore.logout()
      router.push('/login')
    }
  })
}
</script>

<style scoped>
.layout {
  min-height: 100vh;
}

.logo {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 16px;
  border-bottom: 1px solid var(--border-color);
  background: var(--background-color);
}

.logo-img {
  width: 32px;
  height: 32px;
  margin-right: 8px;
}

.logo-text {
  font-size: 16px;
  font-weight: 500;
  color: var(--text-color);
}

.layout-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  background: var(--background-color);
  box-shadow: 0 1px 4px var(--shadow-color-light);
  border-bottom: 1px solid var(--border-color);
}

.header-left {
  display: flex;
  align-items: center;
}

.trigger {
  font-size: 18px;
  margin-right: 16px;
}

.breadcrumb {
  margin-left: 16px;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 6px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: var(--background-color-light);
}

.username {
  margin-left: 8px;
  font-size: 14px;
  color: var(--text-color);
}

.layout-content {
  margin: 20px;
  padding: 24px;
  background: var(--background-color);
  border-radius: 8px;
  box-shadow: 0 1px 3px var(--shadow-color-light);
  border: 1px solid var(--border-color);
  min-height: calc(100vh - 104px);
}
</style>